﻿Imports System.Net
Imports System.IO
Imports System.Security.Permissions

Public Class ConexionFTP

    Const PRE_FTP As String = "ftp://"

    Private _Server As String = ""
    Private _User As String = ""
    Private _Password As String = ""
    Private _Port As String = ""

    ''' <summary>
    ''' Set the parameters of connection
    ''' </summary>
    ''' <param name="Server"></param>
    ''' <param name="User"></param>
    ''' <param name="Password"></param>
    ''' <param name="Port"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal Server As String, ByVal User As String, ByVal Password As String, ByVal Port As String)
        Me._Server = Server
        Me._User = User
        Me._Password = Password
        Me._Port = Port
    End Sub


    ''' <summary>
    ''' This function list the directory passed by parameter
    ''' </summary>
    ''' <param name="directory"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ListFiles(ByVal directory As String) As List(Of String)
        Dim list As New List(Of String)
        list.Clear() 'if the list isn't empty

        Dim FTP As FtpWebRequest = CType(FtpWebRequest.Create(PRE_FTP & Me._Server & "/" & directory & "/"), FtpWebRequest)

        FTP.Credentials = New NetworkCredential(Me._User, Me._Password)
        FTP.KeepAlive = True
        FTP.Method = WebRequestMethods.Ftp.ListDirectory
        Try
            Using Response As FtpWebResponse = FTP.GetResponse()
                Using responseStream As Stream = Response.GetResponseStream
                    Using reader = New StreamReader(responseStream)
                        While Not reader.EndOfStream
                            list.Add(reader.ReadLine())
                        End While
                    End Using
                End Using
            End Using
        Catch ex As Exception
            list.Add(ex.ToString())
            Return list
        End Try

        Return list
    End Function

    ''' <summary>
    ''' Delete a file
    ''' </summary>
    ''' <param name="fichero"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DeleteFile(ByVal fichero As String) As String
        Dim FTP As FtpWebRequest = CType(FtpWebRequest.Create(PRE_FTP & Me._Server & fichero), FtpWebRequest)

        FTP.Credentials = New NetworkCredential(Me._User, Me._Password)
        FTP.Method = WebRequestMethods.Ftp.DeleteFile
        FTP.UsePassive = False

        Try
            Using respuestaFTP As FtpWebResponse = CType(FTP.GetResponse(), FtpWebResponse)
                respuestaFTP.Close()
            End Using
            Return "El archivo ha sido eliminado"
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    ''' <summary>
    ''' Checks if file exist
    ''' </summary>
    ''' <param name="archivo"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ExistFile(ByVal archivo As String) As Boolean

        Dim FTP As FtpWebRequest = CType(FtpWebRequest.Create(PRE_FTP & Me._Server & archivo), FtpWebRequest)

        ' Fijamos el usuario y la contraseña de la petición
        FTP.Credentials = New NetworkCredential(Me._User, Me._Password)

        ' Para saber si el objeto existe, solicitamos la fecha de creación del mismo
        FTP.Method = WebRequestMethods.Ftp.GetDateTimestamp

        FTP.UsePassive = False

        Try
            ' Si el objeto existe, se devolverá True
            Using respuestaFTP As FtpWebResponse = CType(FTP.GetResponse(), FtpWebResponse)
                respuestaFTP.Close()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    ''' <summary>
    ''' Create a directory
    ''' </summary>
    ''' <param name="directorio"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CreateDirectory(ByVal directorio As String) As String
        Dim FTP As FtpWebRequest = CType(FtpWebRequest.Create(PRE_FTP & Me._Server & "/" & directorio), FtpWebRequest)

        ' Fijamos el usuario y la contraseña de la petición
        FTP.Credentials = New NetworkCredential(Me._User, Me._Password)

        ' Seleccionamos el comando que vamos a utilizar: Crear un directorio
        FTP.Method = WebRequestMethods.Ftp.MakeDirectory

        Try
            Using respuestaFTP As FtpWebResponse = CType(FTP.GetResponse(), FtpWebResponse)
                respuestaFTP.Close()
            End Using
            ' Si todo ha ido bien, se devolverá String.Empty
            Return "Directorio creado correctamente"
        Catch ex As Exception
            ' Si se produce algún fallo, se devolverá el mensaje del error
            Return ex.Message
        End Try
    End Function

    ''' <summary>
    ''' Rename a file
    ''' </summary>
    ''' <param name="archivo"></param>
    ''' <param name="archivoNombreNuevo"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function RenameFile(ByVal archivo As String, ByVal archivoNombreNuevo As String) As Boolean

        Dim FTP As FtpWebRequest = CType(FtpWebRequest.Create(PRE_FTP & Me._Server & archivo), FtpWebRequest)

        ' Fijamos el usuario y la contraseña de la petición
        FTP.Credentials = New NetworkCredential(Me._User, Me._Password)

        ' Para saber si el objeto existe, solicitamos la fecha de creación del mismo
        FTP.Method = WebRequestMethods.Ftp.Rename
        FTP.RenameTo = archivoNombreNuevo

        FTP.UsePassive = False

        Try
            ' Si el objeto existe, se devolverá True
            Using respuestaFTP As FtpWebResponse = CType(FTP.GetResponse(), FtpWebResponse)
                respuestaFTP.Close()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    ''' <summary>
    ''' Upload the file
    ''' The params are: 
    ''' path of file in the system, path of file in server, folder of file in server
    ''' Ex, "C:\youpadsfsadf.txt", "/yosadf.txt", "/ruta1/"
    ''' </summary>
    ''' <param name="pathLocalFile"></param>
    ''' <param name="pathRemoteFile"></param>
    ''' <param name="directory"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function UploadFile(ByVal pathLocalFile As String, ByVal pathRemoteFile As String, ByVal directory As String) As String

        Dim infoFichero As New FileInfo(pathLocalFile)

        If Not ExistFile(directory) Then
            CreateDirectory(directory)
        End If

        Dim FTP As FtpWebRequest = CType(FtpWebRequest.Create(PRE_FTP & Me._Server & pathRemoteFile), FtpWebRequest)

        FTP.Credentials = New NetworkCredential(Me._User, Me._Password)

        FTP.KeepAlive = False
        FTP.UsePassive = False

        FTP.Method = WebRequestMethods.Ftp.UploadFile

        FTP.UseBinary = True

        FTP.ContentLength = infoFichero.Length

        Dim longitudBuffer As Integer
        longitudBuffer = 2048
        Dim lector As Byte() = New Byte(2048) {}

        Dim num As Integer

        Dim fs As FileStream
        fs = infoFichero.OpenRead()

        Try
            Dim escritor As Stream
            escritor = FTP.GetRequestStream()
            num = fs.Read(lector, 0, longitudBuffer)

            While (num <> 0)
                escritor.Write(lector, 0, num)
                num = fs.Read(lector, 0, longitudBuffer)
            End While

            escritor.Close()
            fs.Close()
            Return "Archivo subido correctamente"
        Catch ex As Exception
            Return ex.Message
        End Try

    End Function

    ''' <summary>
    ''' Download a file
    ''' </summary>
    ''' <param name="localDirectory"></param>
    ''' <param name="remoteFile"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DownloadFile(ByVal localDirectory As String, ByVal remoteFile As String) As String

        Dim FTP As FtpWebRequest = CType(FtpWebRequest.Create(PRE_FTP & Me._Server & remoteFile), FtpWebRequest)

        FTP.Credentials = New NetworkCredential(Me._User, Me._Password)
        FTP.KeepAlive = False

        FTP.Method = WebRequestMethods.Ftp.DownloadFile
        FTP.UseBinary = True

        FTP.Proxy = Nothing
        FTP.EnableSsl = False


        Dim buffLength As Integer = 2048
        Dim buff(buffLength) As Byte

        Try
            Using response As System.Net.FtpWebResponse = CType(FTP.GetResponse, System.Net.FtpWebResponse)
                Using responseStream As IO.Stream = response.GetResponseStream
                    Using fs As New IO.FileStream(localDirectory & remoteFile, IO.FileMode.Create, FileAccess.ReadWrite, FileShare.None)
                        Dim read As Integer = 0
                        Do
                            read = responseStream.Read(buff, 0, buff.Length)
                            fs.Write(buff, 0, read)
                        Loop Until read = 0
                        responseStream.Close()
                        fs.Flush()
                        fs.Close()
                    End Using
                    responseStream.Close()
                End Using
                response.Close()
            End Using
        Catch ex As Exception
            Return ex.Message
        End Try

        Return "Descarga correcta"

    End Function


    'Private Function DarPermisos(ByVal folder As String) As Boolean

    '    Dim pFolder As New FileIOPermission(FileIOPermissionAccess.AllAccess, folder)

    '    Try
    '        pFolder.Demand()
    '        Return True
    '    Catch ex As Exception

    '    End Try
    '    Return False
    'End Function

End Class

